<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function test_dir($dir)
{
    if(is_dir($dir))
        return true;
    else
    {
        # Essaie creation
        if(!mkdir ($dir, 0777)) return false;
        else return true;
    }
}

function is_image($extension)
{
    if( !strstr($extension, 'jpg') && !strstr($extension, 'jpeg') && !strstr($extension, 'bmp') && !strstr($extension, 'gif') && !strstr($extension, 'png') && !strstr($extension, 'JPG') && !strstr($extension, 'JPEG') && !strstr($extension, 'PNG'))
        return false;
    else
        return true;
}

function upload_annonce_resize($nom, $annonce, $forceimage = true)
{
    $dir = IMMOPHP_ROOT_PATH .'/upload/annonces/'.$annonce;

    if(!test_dir($dir) || !extension_loaded('gd') ):
        return false;
    else:
        // Augmentation allocation memoire
        @ini_set('memory_limit','100M');

        // Recuperation Fichier
        $filename = $_FILES[''. $nom .'']['name'];
        $tmp_file = $_FILES[''. $nom .'']['tmp_name'];

        // Recuperation taille fichier
        $size = getimagesize($tmp_file);
        $extension = substr(strrchr($filename,'.'),1);

        // Verification de l extension
        if( !is_image($extension) ):
            return false;
        endif;

        if($size[0] > 1024):
            // Creation image temporaire
            $image_temporaire = IMMOPHP_ROOT_PATH .'upload/temporaire/'.$filename;
            // Deplacement image temporaire dans dossier tmp
            move_uploaded_file($_FILES[''. $nom .'']['tmp_name'], $image_temporaire);
            //Calcul nouvelle dimension
            $tauxreduction = 1024 / $size[0];
            $newhauteur = $size[1] * $tauxreduction;
            // Creation image en fonction de son type
            if (eregi('jpg', $extension) || eregi('jpeg', $extension) || eregi('JPG', $extension) || eregi('JPEG', $extension) ):
                $src = imagecreatefromjpeg($image_temporaire);
            endif;

            if (eregi('png', $extension) || eregi('PNG', $extension) ):
                $src = imagecreatefrompng($image_temporaire);
            endif;

            $img = imagecreatetruecolor(1024, $newhauteur);

            if (!$img):
                $img = imagecreate(1024, $newhauteur);
            endif;

            imagecopyresampled($img, $src, 0, 0, 0, 0, 1024, $newhauteur, $size[0], $size[1]);

            // nom de la nouvelle image
            $newnameimage = time() . '.'.$extension;
            //Chemin destination nouvelle image
            $temp = IMMOPHP_ROOT_PATH .'upload/annonces/'. $annonce .'/' . $newnameimage;
            // Creation image finale
            if (eregi("jpg", $extension) || eregi("jpeg", $extension) || eregi('JPG', $extension) || eregi('JPEG', $extension) ):
                    imagejpeg($img, $temp);
            elseif(eregi("png", $extension) || eregi('PNG', $extension) ):
                imagepng($img, $temp);
            endif;
            // Supression image tomporaire
            unlink($image_temporaire);

            if (is_file($temp)):
                return $newnameimage;
            else:
                return upload_annonce($nom, $annonce, true);
            endif;


        endif;
            return upload_annonce($nom, $annonce, true);
    endif;
}

function upload_annonce($nom, $annonce, $forceimage = true)
{
    $dir = IMMOPHP_ROOT_PATH .'/upload/annonces/'.$annonce;

    if(test_dir($dir))
    {
        # Dossier OK, Traitement de l upload
        $tmp_file = $_FILES[''. $nom .'']['tmp_name'];                              // Recuperation de l image

        if(!is_image($_FILES[''. $nom .'']['type']))
            return false;

        $name_file = time() . '.' . substr(strrchr($_FILES[''. $nom .'']['name'],'.'),1);
	if(!move_uploaded_file($tmp_file, IMMOPHP_ROOT_PATH .'upload/annonces/'.$annonce .'/'. $name_file))
            return false;
	else
            return $name_file;
    }
    else
        return false;
}
?>
